package com.sqx.modules.app.controller.app;


import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.app.annotation.LoginUser;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoney;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.entity.UserVip;
import com.sqx.modules.app.service.*;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * APP登录授权
 */
@RestController
@RequestMapping("/app/user")
@Api(value = "APP管理", tags = {"APP管理"})
public class AppController {

    @Autowired
    private UserService userService;
    @Autowired
    private AppService appService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserVipService userVipService;
    @Autowired
    private UserMoneyService userMoneyService;
    @Autowired
    private UserMoneyDetailsService userMoneyDetailsService;

    /*
     * 获取所有的师傅信息
     * */
    @RequestMapping(value = "/getAllWork",method = RequestMethod.GET)
    @ResponseBody
    public Result getWorkerUserList() {

        return userService.getAllMaster();
    }

    @RequestMapping(value = "/selectUserByIds", method = RequestMethod.GET)
    @ApiOperation("获取用户详细信息")
    @ResponseBody
    public Result selectUserByIds(Long userId) {
        return Result.success().put("data", userService.selectUserById(userId));
    }

    @Login
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("用户端修改密码")
    public Result updatePwd(@LoginUser UserEntity user, String pwd, String oldPwd) {
        if (!user.getPassword().equals(DigestUtils.sha256Hex(oldPwd))) {
            return Result.error("原始密码不正确！");
        }
        if (pwd.equals(oldPwd)) {
            return Result.error("新密码不能与旧密码相同！");
        }
        user.setPassword(DigestUtils.sha256Hex(pwd));
        userService.updateById(user);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/updatePhone", method = RequestMethod.POST)
    @ApiOperation("用户端换绑手机号")
    @ResponseBody
    public Result updatePhone(@RequestAttribute("userId") Long userId, @RequestParam String phone, @RequestParam String msg, Integer userType) {
        return userService.updatePhone(phone, msg, userId, userType);
    }

    @Login
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    @ApiOperation("用户修改个人信息")
    @ResponseBody
    public Result updateUser(@RequestBody UserEntity userEntity, @RequestAttribute("userId") Long userId) {
        userEntity.setUserId(userId);
        userService.updateById(userEntity);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/updateUserImageUrl", method = RequestMethod.POST)
    @ApiOperation("用户修改头像")
    @ResponseBody
    public Result updateUserImageUrl(@LoginUser UserEntity user, String avatar) {
        user.setAvatar(avatar);
        userService.updateById(user);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/updateUserName", method = RequestMethod.POST)
    @ApiOperation("用户修改昵称")
    @ResponseBody
    public Result updateUserName(@LoginUser UserEntity user, String userName) {
        user.setUserName(userName);
        userService.updateById(user);
        return Result.success();
    }

    @Login
    @RequestMapping(value = "/selectUserById", method = RequestMethod.GET)
    @ApiOperation("获取用户详细信息")
    @ResponseBody
    public Result selectUserById(@LoginUser UserEntity user) {
        if (user != null) {
            if (user.getRate() == null) {
                user.setRate(new BigDecimal(commonInfoService.findOne(206).getValue()));
            }
            if (user.getZhiRate() == null) {
                user.setZhiRate(new BigDecimal(commonInfoService.findOne(207).getValue()));
            }
            if (user.getFeiRate() == null) {
                user.setFeiRate(new BigDecimal(commonInfoService.findOne(208).getValue()));
            }
            if (user.getUserType() == null) {
                if (StringUtils.isNotEmpty(user.getOpenId())) {
                    user.setUserType(1);
                } else if (StringUtils.isNotEmpty(user.getShopOpenId())) {
                    user.setUserType(2);
                }
            }
        }
        userService.updateById(user);
        if (user != null) {
            UserVip userVip = userVipService.selectUserVipByUserId(user.getUserId());
            if (userVip != null) {
                if (userVip != null && userVip.getIsVip() != null && userVip.getIsVip() == 1) {
                    user.setIsVip(1);
                    user.setVipEndTime(userVip.getEndTime());
                }
            }
        }
        return Result.success().put("data", user);
    }

    @Login
    @GetMapping("/selectStaffUser")
    @ApiOperation("查询员工用户信息")
    public Result selectStaffUser(Long userId) {
        return Result.success().put("data", userService.getById(userId));
    }


    @RequestMapping(value = "/selectNewApp", method = RequestMethod.GET)
    @ApiOperation("升级检测")
    @ResponseBody
    public Result selectNewApp() {
        return Result.success().put("data", appService.selectNewApp());
    }

    @RequestMapping(value = "/updateClientId", method = RequestMethod.POST)
    @ApiOperation("绑定ClientId")
    @ResponseBody
    public Result updateClientId(String clientId, Long userId, Integer sysPhone) {
        userService.updateUserClientIdIsNull(clientId);
        UserEntity userEntity = new UserEntity();
        userEntity.setSysPhone(sysPhone);
        userEntity.setUserId(userId);
        userEntity.setClientid(clientId);
        userService.updateById(userEntity);
        return Result.success();
    }

    @Login
    @PostMapping("/insertStaffUser")
    @ApiOperation("关联员工")
    public Result insertStaffUser(@RequestAttribute Long userId, String phone, String msg) {
        return userService.insertStaffUser(userId, phone, msg);
    }

    @Login
    @PostMapping("/exitCompany")
    @ApiOperation("退出公司")
    public Result exitCompany(@RequestAttribute Long userId) {
        UserEntity userEntity = userService.getById(userId);
        userEntity.setCompanyId(0L);
        userService.updateById(userEntity);
        return Result.success();
    }

    @Login
    @PostMapping("/adminExitCompany")
    @ApiOperation("商家踢出员工")
    public Result adminExitCompany(@RequestAttribute Long userId, Long byUserId) {
        UserEntity userEntity = userService.getById(byUserId);
        if (userEntity.getCompanyId().equals(userId)) {
            userEntity.setCompanyId(0L);
            userService.updateById(userEntity);
            return Result.success();
        }
        return Result.error("该用户已不在公司！");
    }

    @Login
    @RequestMapping(value = "/selectUserList", method = RequestMethod.GET)
    @ApiOperation("查询所有用户列表")
    @ResponseBody
    public Result selectUserList(Integer page, Integer limit,
                                 @ApiParam("用户id 手机号 昵称 模糊搜索") String phone,
                                 @ApiParam("性别 1男 2女") Integer sex,
                                 @ApiParam("来源") String platform,
                                 @ApiParam("手机终端") String sysPhone,
                                 @ApiParam("状态 1正常 2封禁") Integer status,
                                 @RequestAttribute Long userId,
                                 Integer isAuthentication, String userName,
                                 @ApiParam("查询员工") String phoneAndUserName,
                                 String invitationCode,
                                 String inviterCode,
                                 Integer isCompany,
                                 Integer isVip) {
        return userService.selectUserPage(page, limit, phone, sex, platform, sysPhone, status, userId, isAuthentication, userName, phoneAndUserName, isCompany, null, null, invitationCode, inviterCode, isVip);
    }


}
